package seatiger.tomcat.catalina.session;

import seatiger.tomcat.catalina.*;
import seatiger.tomcat.util.StringManager;

import java.io.IOException;
import java.util.List;

/**
 *
 */
public abstract class StoreBase implements Lifecycle, Runnable, Store {

    protected Manager manager;

    protected boolean started = false;

    protected static String storeName = "StoreBase";

    protected StringManager sm = StringManager.getManager(Constants.PACKAGE_SESSION);

    protected Thread thread = null;

    protected boolean threadDone = false;

    protected int checkInterval = 60;

    @Override
    public void setManager(Manager manager) {
        this.manager = manager;
    }

    @Override
    public Manager getManager() {
        return manager;
    }

    public String getStoreName() {
        return (storeName);
    }

    @Override
    public void start() throws LifecycleException {
        if (started) {
            throw new LifecycleException(sm.getString(getStoreName() + ".alreadyStarted"));
        }
        started = true;
        threadStart();
    }

    @Override
    public void stop() throws LifecycleException {
        if (!started){
            throw new LifecycleException
                    (sm.getString(getStoreName()+".notStarted"));
        }
        started = false;
        threadStop();
    }

    protected void threadStart() {
        if (thread != null) {
            return;
        }
        threadDone = false;
        thread = new Thread(this);
        thread.setDaemon(true);
        thread.start();
    }

    protected void threadSleep() {
        try {
            Thread.sleep(checkInterval * 1000L);
        } catch (InterruptedException e) {
        }
    }

    protected void threadStop() {
        if (thread == null)
            return;
        threadDone = true;
        thread.interrupt();
        try {
            thread.join();
        } catch (InterruptedException e) {
        }
        thread = null;
    }

    @Override
    public void run() {
        while (!threadDone) {
            threadSleep();
            processExpires();
        }
    }

    public void processExpires() {
        String[] keys = null;
        if(!started) {
            return;
        }
        try {
            keys = keys();
        } catch (IOException e) {
            return;
        }
        for (int i = 0; i < keys.length; i++) {
            try {
                StandardSession session = (StandardSession) load(keys[i]);
                session.isValid();
            }catch (Exception e) {
                try {
                    remove(keys[i]);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    @Override
    public void addLifecycleListener(LifecycleListener listener) {

    }

    @Override
    public void removeLifecycleListener(LifecycleListener listener) {

    }

    @Override
    public List<LifecycleListener> findLifecycleListeners() {
        return null;
    }
}
